package com.bw.flinkstreaming.state2.job2;

import com.bw.flinkstreaming.state2.job1.ValueStateCountAvg;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 *
 * 需求：相同key的单词出现三次就计算平均值
 *   hadoop
 *   hive
 *   hive
 *   hadoop
 *   spark
 *   hadoop  -> 触发计算
 *   hive    -> 触发计算
 *   hadoop
 *   hadoop
 *   hadoop  -> 触发计算
 *
 *   测试代码
 *
 * */
public class ListStateCountAvgTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        /**
         * 初始数据：
         *  100,20
         *  101,20
         *  102,20
         *  100,21
         *  100,160
         *  100,260
         *
         *  结果： 100,67
         * */
        DataStreamSource<Tuple2<Long, Long>> sources = env.fromElements(Tuple2.of(100L, 20L), Tuple2.of(101L, 20L), Tuple2.of(102L, 20L), Tuple2.of(100L, 21L), Tuple2.of(100L, 160L),Tuple2.of(100L, 260L),Tuple2.of(102L, 20L),Tuple2.of(102L, 20L));
        //keyBy分组后，自定义聚合之后逻辑，并且操作状态数据，来完成业务。
        sources.keyBy(0).flatMap(new ListStateCountAvg()).print();
        env.execute("ListStateCountAvgTest");
    }
}
